需要用到共同屬性的時候用 extends

  • call super() 是為了 call 上一層的 constructor
  • 而且需要將 name 傳上去
class Dog {
  constructor(name) {
    this.name = name
  }

  sayHello() {
    console.log(this.name)
  }
}

class BlackDog extends Dog {
  constructor(name) {
    super(name)
    this.sayHello()
  }
  test() {
    console.log('test', this.name)
  }
}
const d = new BlackDog('hello') 
// hello


參考資源


#程式導師實驗計畫第四期 #前端 #inheritance







Related Posts

CS50 IP (Internet Protocol)

CS50 IP (Internet Protocol)

我要成為前端工程師的學習筆記:HTML & CSS 篇 - Box Model、區塊水平置中、文字水平調整 Day7

我要成為前端工程師的學習筆記:HTML & CSS 篇 - Box Model、區塊水平置中、文字水平調整 Day7

3.  React 語法糖:JSX

3. React 語法糖:JSX


Comments